package com.example.current.tool;

import java.util.ArrayList;

/**
 * 利用semaphore强制让多线程变成单线程
 */
public class SemaphoreEp1 {
    static ArrayList arrayList = new ArrayList();
    public static void main(String[] args) throws Exception {
        ThreadLocal<Object> objectThreadLocal = new ThreadLocal<>();
        objectThreadLocal.set(1);
        objectThreadLocal.set(2);

//        System.out.println(objectThreadLocal.get());
//        objectThreadLocal.remove();
//        System.out.println(objectThreadLocal.get());
//        int count = 5000;
//        Semaphore semaphore = new Semaphore(1);
//        CountDownLatch countDownLatch = new CountDownLatch(count);
//        ExecutorService executorService = Executors.newCachedThreadPool();
//        for (int i =0;i<count;i++){
//            final int num = i;
//            executorService.execute(()->{
//                try {
//                    if(semaphore.tryAcquire(1)){
//                        doing(num);
//                        semaphore.release();
//                    }
//                    countDownLatch.countDown();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            });
//        }
//        countDownLatch.await(100, TimeUnit.SECONDS);
//        System.out.println(arrayList.size());
//        executorService.shutdown();
    }
    public static void doing(int i){
        arrayList.add(i);
    }

}
